#ifndef __MOTOR_DRV_SIM
#define __MOTOR_DRV_SIM

#include "stdint-gcc.h"

// x = 32768, LOG2(x) = 15;
#define LOG2(x) \
  ((x) == 65535 ? 16 : \
   ((x) == 2*2*2*2*2*2*2*2*2*2*2*2*2*2*2 ? 15 : \
    ((x) == 2*2*2*2*2*2*2*2*2*2*2*2*2*2 ? 14 : \
     ((x) == 2*2*2*2*2*2*2*2*2*2*2*2*2 ? 13 : \
      ((x) == 2*2*2*2*2*2*2*2*2*2*2*2 ? 12 : \
       ((x) == 2*2*2*2*2*2*2*2*2*2*2 ? 11 : \
        ((x) == 2*2*2*2*2*2*2*2*2*2 ? 10 : \
         ((x) == 2*2*2*2*2*2*2*2*2 ? 9 : \
          ((x) == 2*2*2*2*2*2*2*2 ? 8 : \
           ((x) == 2*2*2*2*2*2*2 ? 7 : \
            ((x) == 2*2*2*2*2*2 ? 6 : \
             ((x) == 2*2*2*2*2 ? 5 : \
              ((x) == 2*2*2*2 ? 4 : \
               ((x) == 2*2*2 ? 3 : \
                ((x) == 2*2 ? 2 : \
                 ((x) == 2 ? 1 : \
                  ((x) == 1 ? 0 : -1)))))))))))))))))




#define INT_ABS(a) ((a > 0) ? a : (-a))

typedef struct
{
  int16_t hcos; //cos value
  int16_t hsin; //sin value
} TrigComponents;


typedef struct
{
    int16_t alpha;
    int16_t beta;
}AlphaBeta_t;

typedef struct 
{
  int16_t d;
  int16_t q;
}Qd_t;

typedef struct
{
  int16_t a;
  int16_t b;
} Ab_t;

typedef struct 
{
  int32_t u;
  int32_t v;
  int32_t w;
  uint8_t k; 
  uint8_t sector;
  uint32_t tx;
  uint32_t ty;
  uint32_t tz;
  int32_t sa;
  int32_t sb;
  int32_t sc;

  uint32_t ta;
  uint32_t tb;
  uint32_t tc;
}Uvw_t;


//struct of motor parameter and control method
typedef struct
{
    struct  //motor current
    {
        int16_t iu;     //motor current iu
        int16_t iv;     //motor current iv
        int16_t iw;     //motor current iw
        int16_t ialpha; //ialpha after clark
        int16_t ibeta;  //ibeta after clark
        int16_t id;     //id after park
        int16_t iq;     //iq after park
    }current;
    struct 
    {
        uint16_t raw;           //raw data form ic through spi interface
        int16_t  m_angle;       //machnical angle after minus raw data at '0' degree
        int16_t  hm_angle;
        int16_t  e_angle;       //electric angle corrosponder to machnical angle 
        int16_t  he_angle;
    }encoder;

    struct 
    {
        uint8_t control_mode;       //torque speed or position mode
        int16_t id_ref;             //reference value input of id
        int16_t iq_ref;             //reference value input of iq
        int16_t torque_ref;         //reference value input of torque
        uint16_t speed_ref;         //reference value input of speed
    }ctl_param;

    struct 
    {
        TrigComponents (*trig_functions)(int16_t angle);//calculate sin and cos value through look up table
    }motor_func;

}MOTOR;



TrigComponents trig_functions( int16_t hangle );
#endif /* MOTOR_USR_MATH_H_ */






